package com.artisan.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.artisan.dao.ClassDao;
import com.artisan.dao.StudentDao;
import com.artisan.model.Student;
import com.artisan.model.StudentClass;
import com.artisan.util.StringUtil;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;

public class AddStudentFrm extends JInternalFrame {
	private JTextField studentNameTextField;
	private JPasswordField studentPasswordField;
	private JComboBox studentClassComboBox;
	private ButtonGroup sexButtonGroup;
	private JRadioButton studentSexManRadioButton;
	private JRadioButton studentSexFemaleRadioButton;
	private JRadioButton studentSexUnkonwRadioButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddStudentFrm frame = new AddStudentFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddStudentFrm() {
		setFrameIcon(new ImageIcon(AddStudentFrm.class.getResource("/com/artisan/image/学生管理.png")));
		setClosable(true);
		setIconifiable(true);
		
		setTitle("添加学生");
		setBounds(100, 100, 610, 388);

		JLabel lblNewLabel = new JLabel("学生姓名:");
		lblNewLabel.setIcon(new ImageIcon(AddStudentFrm.class.getResource("/com/artisan/image/学生管理.png")));
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		studentNameTextField = new JTextField();
		studentNameTextField.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("所属班级:");
		lblNewLabel_1.setIcon(new ImageIcon(AddStudentFrm.class.getResource("/com/artisan/image/班级名称.png")));
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		studentClassComboBox = new JComboBox();
		studentClassComboBox.setModel(new DefaultComboBoxModel(new String[] {}));

		JLabel lblNewLabel_2 = new JLabel("登录密码:");
		lblNewLabel_2.setIcon(new ImageIcon(AddStudentFrm.class.getResource("/com/artisan/image/密码.png")));
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		studentPasswordField = new JPasswordField();

		JLabel lblNewLabel_3 = new JLabel("学生性别:");
		lblNewLabel_3.setIcon(new ImageIcon(AddStudentFrm.class.getResource("/com/artisan/image/性别.png")));
		lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		studentSexManRadioButton = new JRadioButton("男");
		studentSexManRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		studentSexManRadioButton.setSelected(true);

		studentSexFemaleRadioButton = new JRadioButton("女");
		studentSexFemaleRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		studentSexUnkonwRadioButton = new JRadioButton("保密");
		studentSexUnkonwRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		sexButtonGroup = new ButtonGroup();
		sexButtonGroup.add(studentSexManRadioButton);
		sexButtonGroup.add(studentSexFemaleRadioButton);
		sexButtonGroup.add(studentSexUnkonwRadioButton);

		JButton btnNewButton = new JButton("确认");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				studentAddAct(ae);
			}
		});
		btnNewButton.setIcon(new ImageIcon(AddStudentFrm.class.getResource("/com/artisan/image/确认.png")));
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				resetValue(ae);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(AddStudentFrm.class.getResource("/com/artisan/image/重置.png")));
		btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout
				.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
						.createSequentialGroup().addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(
								groupLayout
										.createSequentialGroup().addGap(130).addGroup(groupLayout
												.createParallelGroup(Alignment.TRAILING, false)
												.addComponent(
														lblNewLabel_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
												.addComponent(lblNewLabel_2, Alignment.LEADING,
														GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(lblNewLabel_1, Alignment.LEADING,
														GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(lblNewLabel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
										.addGap(37)
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
												.addGroup(groupLayout.createSequentialGroup()
														.addComponent(studentSexManRadioButton).addGap(30)
														.addComponent(studentSexFemaleRadioButton).addGap(18)
														.addComponent(studentSexUnkonwRadioButton))
												.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
														.addComponent(studentNameTextField, Alignment.LEADING)
														.addComponent(studentClassComboBox, Alignment.LEADING, 0,
																GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
														.addComponent(studentPasswordField, Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))))
								.addGroup(groupLayout.createSequentialGroup().addGap(168)
										.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 97,
												GroupLayout.PREFERRED_SIZE)
										.addGap(41).addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 97,
												GroupLayout.PREFERRED_SIZE)))
						.addContainerGap(145, Short.MAX_VALUE)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup().addGap(46)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel)
						.addComponent(studentNameTextField, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
				.addGap(36)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_1)
						.addComponent(studentClassComboBox, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
				.addGap(38)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_2)
						.addComponent(studentPasswordField, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
				.addGap(41)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_3)
						.addComponent(studentSexManRadioButton).addComponent(studentSexUnkonwRadioButton)
						.addComponent(studentSexFemaleRadioButton))
				.addPreferredGap(ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
				.addGap(25)));
		getContentPane().setLayout(groupLayout);
		setStudentClassInfo();
	}

	protected void resetValue(ActionEvent ae) {
		// TODO 自动生成的方法存根
		studentNameTextField.setText("");
		studentPasswordField.setText("");
		studentClassComboBox.setSelectedIndex(0);
		sexButtonGroup.clearSelection();
		studentSexManRadioButton.setSelected(true);
	}

	protected void studentAddAct(ActionEvent ae) {
		// TODO 自动生成的方法存根
		String studentName = studentNameTextField.getText().toString();
		String studentPassword = String.valueOf(studentPasswordField.getPassword());
		if (StringUtil.isEmpty(studentName)) {
			JOptionPane.showMessageDialog(this, "请填写学生姓名!");
			return;
		}
		if (StringUtil.isEmpty(studentPassword)) {
			JOptionPane.showMessageDialog(this, "请填写密码!");
			return;
		}
		StudentClass sc = (StudentClass) studentClassComboBox.getSelectedItem();
		String sex = studentSexManRadioButton.isSelected() ? studentSexManRadioButton.getText()
				: (studentSexFemaleRadioButton.isSelected() ? studentSexFemaleRadioButton.getText()
						: studentSexUnkonwRadioButton.getText());
		Student student = new Student();
		student.setName(studentName);
		student.setClassId(sc.getId());
		student.setPassword(studentPassword);
		student.setSex(sex);
		StudentDao studentDao = new StudentDao();
		if (studentDao.addStudent(student)) {
			JOptionPane.showMessageDialog(this, "添加成功!");
		} else {
			JOptionPane.showMessageDialog(this, "添加失败!");
		}
		resetValue(ae);
	}

	private void setStudentClassInfo() {
		ClassDao classDao = new ClassDao();
		List<StudentClass> classList = classDao.getClassList(new StudentClass());
		for (StudentClass sc : classList) {
			studentClassComboBox.addItem(sc);
		}
		classDao.closeDao();
	}
}